{extend name="../../base/view/common/base" /}
<!-- 主体 -->
{block name="body"}
<div class="p-3">
    <form class="layui-form gg-form-bar border-x border-t">
        <div class="layui-input-inline" style="width:300px;">
            <input type="text" name="name" placeholder="员工名称" class="layui-input" autocomplete="off" />
        </div>
        <div class="layui-input-inline" style="width:150px;">
            <button class="layui-btn layui-btn-normal" lay-submit="" lay-filter="webform"><i class="layui-icon layui-icon-search mr-1"></i>搜索</button>
            <button type="reset" class="layui-btn layui-btn-reset" lay-filter="reset">清空</button>
        </div>
    </form>
    <table class="layui-hide" id="test" lay-filter="test"></table>
</div>

<script type="text/html" id="toolbarDemo">
  <div class="layui-btn-container">
    <span class="layui-btn layui-btn-sm" title="添加客户" lay-event="add">+ 添加员工</span>
  </div>
</script>

{/block}
<!-- /主体 -->

<!-- 脚本 -->
{block name="script"}
	<script>
	const moduleInit = ['tool'];

    //表头工具栏事件

	function gouguInit() {
		var table = layui.table, tool = layui.tool, form = layui.form;

        table.on('toolbar(test)', function(obj){
            if (obj.event === 'add') {
                tool.side("/home/user/add");
                return;
            }
        });

		layui.pageTable = table.render({
			elem: '#test'
			,toolbar: '#toolbarDemo'
			,defaultToolbar: false
			,title:'员工列表'
			,url: "/home/user/index"
			,page: false
			,cellMinWidth: 80
			,cols: [[
					{field:'id',width:80, title: 'ID号', align:'center'}
					,{field:'name',title: '员工名称'}
					,{field:'code',title: '员工编号'}
					,{field:'fmt_start_time',title: '入职时间'}
					,{field:'educational',title: '学历'}
					,{field:'major',title: '专业'}
					,{field:'position',title: '岗位'}
					,{field:'status', title: '状态',width:80,align:'center',templet: function(d){
						var html1='<span class="green">进行中</span>';
						var html2='<span class="red">完结</span>';
						if(d.status==1){
							return html1;
						}
						else{
							return html2;
						}
					}}
					,{width:150,title: '操作', align:'center',templet: function(d){
						var html='';
						var btn='<a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="edit">编辑</a>';
						var btn1='<a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="disable">完结</a>';
						var btn2='<a class="layui-btn layui-btn-xs" lay-event="open">启用</a>';
						if(d.status==1){
							html = '<div class="layui-btn-group">'+btn+btn1+'<a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>'+'</div>';
						}
						else{
							html = '<div class="layui-btn-group">'+btn+btn2+'<a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>'+'</div>';
						}
						return html;
					}}
				]]
			});
			
			table.on('tool(test)',function (obj) {
				if(obj.event === 'edit'){
                    tool.side('/home/user/add?id='+obj.data.id);
                    return;
				}
				if(obj.event === 'disable'){
					layer.confirm('确定要完结该员工吗?', {icon: 3, title:'提示'}, function(index){
						let callback = function (e) {
							layer.msg(e.msg);
							if (e.code == 0) {
								layui.pageTable.reload();
							}
						}
						tool.post("/home/user/opertion", { id: obj.data.id,status: 2,title: obj.data.title}, callback);
						layer.close(index);
					});
				}
				if(obj.event === 'open'){
					layer.confirm('确定修改员工状态吗?', {icon: 3, title:'提示'}, function(index){
						let callback = function (e) {
							layer.msg(e.msg);
							if (e.code == 0) {
								layui.pageTable.reload();
							}
						}
						tool.post("/home/user/opertion", { id: obj.data.id,status: 1,title: obj.data.title}, callback);
						layer.close(index);
					});
				}

                if (obj.event === 'del') {
                    layer.confirm('确定要删除吗？', { icon: 3, title: '提示' }, function (index) {
                        let callback = function (e) {
                            layer.msg(e.msg);
                            if (e.code == 0) {
                                layui.pageTable.reload();
                            }
                        }
                        tool.post("/home/user/delete", { id: obj.data.id }, callback);
                        layer.close(index);
                    });
                }
			});
			
			$('body').on('click','.addNew',function(){
				addExpense(0,'');	
			});
			
			function addExpense(id,val){
				var title = '新增员工';
				if(id>0){
					title = '编辑员工';
				}
				layer.prompt({
					title: title,
					value: val,
					yes: function(index, layero) {
						// 获取文本框输入的值
						var value = layero.find(".layui-layer-input").val();
						if (value) {
							let callback = function (e) {
								layer.msg(e.msg);
								if (e.code == 0) {
									layui.pageTable.reload();				
								}
							}
							tool.post("/home/user/add", {id: id,title: value}, callback);
							layer.close(index);
						} else {
							layer.msg('请填写员工名称');
						}
					}
				})
			}


        //监听搜索提交
        form.on('submit(webform)', function (data) {
            if (data.field.name) {
                layui.pageTable.reload({
                    where: {
                        name: data.field.name
                    },
                    page: {
                        curr: 1
                    }
                });
            } else {
                location.reload();
            }
            return false;
        });
		}
	</script>
{/block}
<!-- /脚本 -->